package servlet.edt;

import bdd.DBLink;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author redaniel
 */
public class SuppCreneau extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    private String DELETE_CRENEAU = "DELETE FROM CRENEAU WHERE NUMSALLE = ? AND HORAIREDEBUT = ? AND HORAIREFIN = ? AND IDDATE = ?";
    private String DELETE_CRENEAU_PERIOD = "DELETE FROM CRENEAU WHERE NUMSALLE = ? AND HORAIREDEBUT = ? AND HORAIREFIN = ? AND IDDATE >= ? AND IDDATE <= ? AND NUMEC = ?";

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            
            String j = (request.getParameter("jours") == null) ? "" : request.getParameter("jours");
            String sal = (request.getParameter("salle") == null) ? "" : request.getParameter("salle");
            Short horaired = (request.getParameter("horairedebut") == null) ? 0 : Short.parseShort(request.getParameter("horairedebut"));
            Short horairef = (request.getParameter("horairefin") == null) ? 0 : Short.parseShort(request.getParameter("horairefin"));
            String dat = (request.getParameter("date") == null) ? "" : request.getParameter("date");
            String cours = (request.getParameter("EC") == null) ? "" : request.getParameter("EC");

            String NameSalle="";
            String NameEc="";

            DBLink dbLink = new DBLink();
            dbLink.connexion();

            String sq2 = "SELECT LIEU FROM Salle WHERE numsalle = ?";
            dbLink.setPst(dbLink.getConnection().prepareStatement(sq2));
            dbLink.getPst().setString(1, sal);
            dbLink.setRs(dbLink.getPst().executeQuery());

            while (dbLink.getRs().next()) {
                NameSalle = dbLink.getRs().getString("LIEU");
            }

            String sql = "SELECT LIBELLE FROM EC WHERE NUMUE = ?";
            dbLink.setPst(dbLink.getConnection().prepareStatement(sql));
            dbLink.getPst().setString(1, cours);
            dbLink.setRs(dbLink.getPst().executeQuery());

            while (dbLink.getRs().next()) {
                NameEc = dbLink.getRs().getString("LIBELLE");
            }


            //Suppression

            if (j.equals("") || sal.equals("") || horaired == 0 || horairef == 0)
            {
                out.println("<p>Suppression impossible : Il manque des informations. </p>");
            }
            else{
                
                if (dat.equals(""))
                {
                    //Suppression d'un seul créneau

                    dbLink.connexion();

                    j += " 00:00:00";

                    dbLink.setPst(dbLink.getConnection().prepareStatement(DELETE_CRENEAU));
                    dbLink.getPst().setString(1, sal);
                    dbLink.getPst().setString(2, ""+horaired);
                    dbLink.getPst().setString(3, ""+horairef);
                    dbLink.getPst().setString(4, j);
                    dbLink.getPst().executeQuery();

                    out.println("<p><font color=\"green\">Suppression effectuée du créneau : </font></p>");

                    out.println("\n- Date : " + j.substring(0,10));
                    out.println("<br>");
                    out.println("\n- Début : " + horaired);
                    out.println("<br>");
                    out.println("\n- Fin : " + horairef);
                    out.println("<br>");
                    out.println("\n- Salle : " + NameSalle);
                    out.println("<br>");
                    out.println("\n- EC : " +  NameEc);
                }
                else
                {
                    //modification des dates de début et fin de période pour permettre la comparaison
                    String str1 = dat.substring(0,2);
                    String str2 = dat.substring(3,5);
                    String str3 = dat.substring(6);
                    dat = str3 + "-" + str2 + "-" + str1;

                    String j1 = j.substring(0,2);
                    String j2 = j.substring(3,5);
                    String j3 = j.substring(6);
                    j = j3 + "-" + j2 + "-" + j1;

                    if (cours.equals("") || dat.compareTo(j) <  0)
                    {

                        dat = str1 + "-" + str2 + "-" + str3;
                        j = j1 + "-" + j2 + "-" + j3;

                        out.println("<p><font color=\"red\">Suppression impossible : L'EC et/ou la période sont invalide. </font></p>");
                        out.println("\n- EC : " +  NameEc);
                        out.println("<br>");
                        out.println("\n- Début période : " + j.substring(0,10));
                        out.println("<br>");
                        out.println("\n- Fin période : " + dat.substring(0,10));
                        
                    }
                    else
                    {

                        //Remet les dates dans le bon sens
                        dat = str1 + "-" + str2 + "-" + str3;
                        j = j1 + "-" + j2 + "-" + j3;
                        
                        //Suppression d'un créneau tous les jours entre deux dates

                        dbLink.connexion();

                        j += " 00:00:00";

                        dbLink.setPst(dbLink.getConnection().prepareStatement(DELETE_CRENEAU_PERIOD));
                        dbLink.getPst().setString(1, sal);
                        dbLink.getPst().setString(2, ""+horaired);
                        dbLink.getPst().setString(3, ""+horairef);
                        dbLink.getPst().setString(4, j);
                        dbLink.getPst().setString(5, dat);
                        dbLink.getPst().setString(6, cours);
                        dbLink.getPst().executeQuery();

                        out.println("<p><font color=\"green\">Suppression effectuée du créneau pour la période saisie. </font></p>");

                        out.println("\n- Début période : " + j.substring(0,10));
                        out.println("<br>");
                        out.println("\n- Fin période : " + dat.substring(0,10));
                        out.println("<br>");
                        out.println("\n- Début : " + horaired);
                        out.println("<br>");
                        out.println("\n- Fin : " + horairef);
                        out.println("<br>");
                        out.println("\n- Salle : " + NameSalle);
                        out.println("<br>");
                        out.println("\n- EC : " + NameEc);

                    }
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace(out);
            out.println("<p> <font color=\"red\"> Suite à un problèmem technique, vote demande ne peut être traitée.</font> </p>");
            out.println("<p> Détails : " + e.getMessage() + "</p>");
        }
        finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
